<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
			<el-form-item label="用户id" prop="userId">
				<el-input v-model="queryParams.userId" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="商品id" prop="goodsId">
				<el-input v-model="queryParams.goodsId" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="商品名称" prop="goodsName">
				<el-input v-model="queryParams.goodsName" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="商品数量" prop="goodsNum">
				<el-input type="number" v-model="queryParams.goodsNum" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="积分" prop="integral">
				<el-input type="number" v-model="queryParams.integral" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="兑换码" prop="redemptionCode">
				<el-input v-model="queryParams.redemptionCode" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="手机号" prop="phone">
				<el-input type="number" v-model="queryParams.phone" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="配送地区" prop="expressArea">
				<el-input type="number" v-model="queryParams.expressArea" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="配送地址" prop="expressAddr">
				<el-input type="number" v-model="queryParams.expressAddr" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="快递公司" prop="expressName">
				<el-input type="number" v-model="queryParams.expressName" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="快递单号" prop="expressCode">
				<el-input v-model="queryParams.expressCode" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="是否支付" prop="isPay">
				<el-select v-model="queryParams.isPay" placeholder="请选择" clearable>
					<el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
				</el-select>
			</el-form-item>
			<el-form-item label="订单状态" prop="orderStatus">
				<el-select v-model="queryParams.orderStatus" placeholder="请选择" clearable>
					<el-option v-for="dict in pm_order_status" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
				</el-select>
			</el-form-item>
			<el-form-item label="部门id" prop="deptId">
				<el-input v-model="queryParams.deptId" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['pm:pmStoreOrder:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['pm:pmStoreOrder:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['pm:pmStoreOrder:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['pm:pmStoreOrder:export']">导出</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="pmStoreOrderList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="用户id" align="center" prop="userId" v-if="columnVisible('userId')"/>
			<el-table-column label="商品id" align="center" prop="goodsId" v-if="columnVisible('goodsId')"/>
			<el-table-column label="商品名称" align="center" prop="goodsName" v-if="columnVisible('goodsName')"/>
			<el-table-column label="商品图片" align="center" prop="goodsPic" width="100" v-if="columnVisible('goodsPic')">
				<template #default="scope">
					<image-preview :src="scope.row.goodsPic" :width="50" :height="50"/>
				</template>
			</el-table-column>
			<el-table-column label="商品数量" align="center" prop="goodsNum" v-if="columnVisible('goodsNum')"/>
			<el-table-column label="积分" align="center" prop="integral" v-if="columnVisible('integral')"/>
			<el-table-column label="兑换码" align="center" prop="redemptionCode" v-if="columnVisible('redemptionCode')"/>
			<el-table-column label="手机号" align="center" prop="phone" v-if="columnVisible('phone')"/>
			<el-table-column label="配送地区" align="center" prop="expressArea" v-if="columnVisible('expressArea')"/>
			<el-table-column label="配送地址" align="center" prop="expressAddr" v-if="columnVisible('expressAddr')"/>
			<el-table-column label="快递公司" align="center" prop="expressName" v-if="columnVisible('expressName')"/>
			<el-table-column label="快递单号" align="center" prop="expressCode" v-if="columnVisible('expressCode')"/>
			<el-table-column label="是否支付" align="center" prop="isPay" v-if="columnVisible('isPay')">
				<template #default="scope">
					<dict-tag :options="sys_yes_no" :value="scope.row.isPay"/>
				</template>
			</el-table-column>
			<el-table-column label="订单状态" align="center" prop="orderStatus" v-if="columnVisible('orderStatus')">
				<template #default="scope">
					<dict-tag :options="pm_order_status" :value="scope.row.orderStatus"/>
				</template>
			</el-table-column>
			<el-table-column label="部门id" align="center" prop="deptId" v-if="columnVisible('deptId')"/>
			<el-table-column label="备注" align="center" prop="remark" v-if="columnVisible('remark')"/>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
				<template #default="scope">
					<el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['pm:pmStoreOrder:query']" v-noPermi="['pm:pmStoreOrder:edit']">查看</el-button>
					<el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['pm:pmStoreOrder:edit']">修改</el-button>
					<el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['pm:pmStoreOrder:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList"/>

		<!-- 添加或修改积分商城订单对话框 -->
		<el-dialog :title="title" v-model="open" width="500px" append-to-body>
			<el-form ref="pmStoreOrderRef" :model="form" :rules="rules" label-width="80px" :disabled="!isInsert && !isEdit">
				<el-form-item label="用户id" prop="userId">
					<el-input v-model="form.userId" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="商品id" prop="goodsId">
					<el-input v-model="form.goodsId" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="商品名称" prop="goodsName">
					<el-input v-model="form.goodsName" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="商品图片" prop="goodsPic">
					<image-upload v-model="form.goodsPic" />
				</el-form-item>
				<el-form-item label="商品数量" prop="goodsNum">
					<el-input-number v-model="form.goodsNum" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="积分" prop="integral">
					<el-input-number v-model="form.integral" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="兑换码" prop="redemptionCode">
					<el-input v-model="form.redemptionCode" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="手机号" prop="phone">
					<el-input-number v-model="form.phone" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="配送地区" prop="expressArea">
					<el-input-number v-model="form.expressArea" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="配送地址" prop="expressAddr">
					<el-input-number v-model="form.expressAddr" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="快递公司" prop="expressName">
					<el-input-number v-model="form.expressName" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="快递单号" prop="expressCode">
					<el-input v-model="form.expressCode" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="是否支付" prop="isPay">
					<el-radio-group v-model="form.isPay" >
						<el-radio v-for="dict in sys_yes_no" :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="订单状态" prop="orderStatus">
					<el-radio-group v-model="form.orderStatus" >
						<el-radio v-for="dict in pm_order_status" :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="部门id" prop="deptId">
					<el-input v-model="form.deptId" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" type="textarea" placeholder="请输入" />
				</el-form-item>
			</el-form>
			<template #footer v-if="isEdit || isInsert">
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="PmStoreOrder">
import { listPmStoreOrder, getPmStoreOrder, delPmStoreOrder, addPmStoreOrder, updatePmStoreOrder } from "@/api/pm/pmStoreOrder";

const { proxy } = getCurrentInstance();
const { pm_order_status, sys_yes_no } = proxy.useDict('pm_order_status', 'sys_yes_no');

const pmStoreOrderList = ref([]);
const isEdit = ref(false);
const isInsert = ref(false);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
// 列显隐信息(able表示列是否可用，visible用于用户控制显隐)
const columns = ref([
	{label: `id`, able: false, visible: false, key: 'id'},
	{label: `用户id`, able: true, visible: true, key: 'userId'},
	{label: `商品id`, able: true, visible: true, key: 'goodsId'},
	{label: `商品名称`, able: true, visible: true, key: 'goodsName'},
	{label: `商品图片`, able: true, visible: true, key: 'goodsPic'},
	{label: `商品数量`, able: true, visible: true, key: 'goodsNum'},
	{label: `积分`, able: true, visible: true, key: 'integral'},
	{label: `兑换码`, able: true, visible: true, key: 'redemptionCode'},
	{label: `手机号`, able: true, visible: true, key: 'phone'},
	{label: `配送地区`, able: true, visible: true, key: 'expressArea'},
	{label: `配送地址`, able: true, visible: true, key: 'expressAddr'},
	{label: `快递公司`, able: true, visible: true, key: 'expressName'},
	{label: `快递单号`, able: true, visible: true, key: 'expressCode'},
	{label: `是否支付`, able: true, visible: true, key: 'isPay'},
	{label: `订单状态`, able: true, visible: true, key: 'orderStatus'},
	{label: `部门id`, able: true, visible: true, key: 'deptId'},
	{label: `创建者`, able: false, visible: false, key: 'createBy'},
	{label: `创建时间`, able: false, visible: false, key: 'createTime'},
	{label: `更新者`, able: false, visible: false, key: 'updateBy'},
	{label: `更新时间`, able: false, visible: false, key: 'updateTime'},
	{label: `备注`, able: true, visible: true, key: 'remark'},
]);

const data = reactive({
	form: {},
	queryParams: {
		pageNum: 1,
		pageSize: 10,
		params: {},
		userId: null,
		goodsId: null,
		goodsName: null,
		goodsNum: null,
		integral: null,
		redemptionCode: null,
		phone: null,
		expressArea: null,
		expressAddr: null,
		expressName: null,
		expressCode: null,
		isPay: null,
		orderStatus: null,
		deptId: null,
	},
	rules: {
		goodsId: [{ required: true, message: "商品id不能为空", trigger: "blur" }],
		goodsName: [{ required: true, message: "商品名称不能为空", trigger: "blur" }],
		goodsPic: [{ required: true, message: "商品图片不能为空", trigger: "blur" }],
		goodsNum: [{ required: true, message: "商品数量不能为空", trigger: "blur" }],
	}
});

const { queryParams, form, rules } = toRefs(data);

/** 列显隐 */
function columnVisible(key) {
	return proxy.checkColumn(columns.value, key);
}

/** 查询积分商城订单列表 */
function getList() {
	loading.value = true;
	listPmStoreOrder(queryParams.value).then(response => {
		pmStoreOrderList.value = response.rows;
		total.value = response.total;
		loading.value = false;
	});
}

// 取消按钮
function cancel() {
	open.value = false;
	reset();
}

// 表单重置
function reset() {
	form.value = {
		id: null,
		userId: null,
		goodsId: null,
		goodsName: null,
		goodsPic: null,
		goodsNum: null,
		integral: null,
		redemptionCode: null,
		phone: null,
		expressArea: null,
		expressAddr: null,
		expressName: null,
		expressCode: null,
		isPay: 0,
		orderStatus: 0,
		deptId: null,
		createBy: null,
		createTime: null,
		updateBy: null,
		updateTime: null,
		remark: null
	};
	proxy.resetForm("pmStoreOrderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
	queryParams.value.pageNum = 1;
	getList();
}

/** 重置按钮操作 */
function resetQuery() {
	proxy.resetForm("queryRef");
	handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
	ids.value = selection.map(item => item.id);
	single.value = selection.length != 1;
	multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
	reset();
	open.value = true;
	title.value = "添加积分商城订单";
	isInsert.value = true;
	isEdit.value = false;
}

/** 查看按钮操作 */
function handleView(row) {
	isEdit.value = false;
	title.value = "查看积分商城订单";
	getInfo(row);
}

/** 修改按钮操作 */
function handleUpdate(row) {
	isEdit.value = true;
	title.value = "修改积分商城订单";
	getInfo(row);
}

function getInfo(row) {
	reset();
	const _id = row.id || ids.value
	isInsert.value = false;
	proxy.modal.loading("加载中");
	getPmStoreOrder(_id).then(response => {
		form.value = response.data;
		open.value = true;
		proxy.modal.closeLoading();
	}).catch(err => {
		Promise.reject(err);
		proxy.modal.closeLoading();
	});
}

/** 提交按钮 */
const pmStoreOrderRef = ref(null)
function submitForm() {
	pmStoreOrderRef.value.validate(valid => {
		if (valid) {
			if (form.value.id != null) {
				proxy.modal.loading("提交中");
				updatePmStoreOrder(form.value).then(response => {
					proxy.modal.msgSuccess("修改成功");
					open.value = false;
					getList();
					proxy.modal.closeLoading();
				}).catch(err=>{
					proxy.modal.closeLoading();
					Promise.reject(err);
				});
			} else {
				proxy.modal.loading("提交中");
				addPmStoreOrder(form.value).then(response => {
					proxy.modal.msgSuccess("新增成功");
					open.value = false;
					getList();
					proxy.modal.closeLoading();
				}).catch(err=>{
					proxy.modal.closeLoading();
					Promise.reject(err);
				});
			}
		}
	});
}

/** 删除按钮操作 */
function handleDelete(row) {
	const _ids = row.id || ids.value;
	proxy.modal.confirm('是否确认删除积分商城订单编号为"' + _ids + '"的数据项？').then(function() {
		proxy.modal.loading("删除中");
		return delPmStoreOrder(_ids);
	}).then(() => {
		getList();
		proxy.modal.msgSuccess("删除成功");
		proxy.modal.closeLoading();
	}).catch(() => {
		proxy.modal.closeLoading();
	});
}

/** 导出按钮操作 */
function handleExport() {
	proxy.download.post('pm/pmStoreOrder/export', {
		...queryParams.value
	}, `pmStoreOrder_${new Date().getTime()}.xlsx`)
}

getList();
</script>
